#include "PlayState.h"


PlayState::PlayState(GameContext& gc)
	:GameState(gc)
{
}


PlayState::~PlayState()
{
}


void PlayState::Load() 
{
	mCharacter.Load();
	mCharacter.SetPosition(SVector2(30.0f,40.0f));

	char levelName[256];
	char texturePackName[256];
	sprintf_s(levelName, 256, "level0%d.txt", mGameContext.GetLevel() );
	sprintf_s(texturePackName, 256, "texturepack0%d.txt", mGameContext.GetLevel() );
	mMap.Load(levelName, texturePackName);
}

void PlayState::Unload() 
{
	mMap.Unload();
	mCharacter.Unload();
}

NextState PlayState::Update(float deltaTime)
{
	
	mCharacter.Update(deltaTime, mMap);

	NextState nextState = None;
	if(Input_IsKeyPressed(Keys::ESCAPE))
	{
		nextState = Frontend;
	}
	return nextState;
}

void PlayState::Render() 
{
	const int kWinWidth = IniFile_GetInt("WinWidth", 800);
	const int kWinHeight = IniFile_GetInt("WinHeight", 600);

	SVector2 viewTarget = mCharacter.GetPosition();
	SVector2 viewOffset;
	viewOffset.x = viewTarget.x - (kWinWidth * 0.5f);
	viewOffset.y = viewTarget.y - (kWinHeight * 0.5f);

	SVector2 limit;
	limit.x = (float)mMap.GetWidth() - kWinWidth;
	limit.y = (float)mMap.GetHeight() - kWinHeight;

	viewOffset.x = Clamp(viewOffset.x, 0.0f, limit.x);
	viewOffset.y = Clamp(viewOffset.y, 0.0f, limit.y);
	mMap.Render(viewOffset);
	mCharacter.Render(viewOffset);
}